define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cartorderitem/index',
                    add_url: 'cartorderitem/add',
                    edit_url: 'cartorderitem/edit',
                    del_url: 'cartorderitem/del',
                    multi_url: 'cartorderitem/multi',
                    table: 'cart_order_item',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.username', title: '用户', operate: 'LIKE'},
                        // {field: 'order_id', title: __('Order_id')},
                        {field: 'order_no', title: __('Order_no')},
                        {field: 'status', title: __('Status'), visible:false, searchList: {"1":"入单中", "2":"已出局"}},
                        {field: 'status_text', title: __('Status'), operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'out_time', title: __('Out_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'admin.username', title: __('Admin_id'),operate:"LIKE"},
                        {field: 'remark', title: __('Remark')},
                        {field: 'out_type', title: __('Out_type'), visible:false, searchList: {"1":"后台出局","2":"用户申请"}},
                        {field: 'out_type_text', title: __('Out_type'), operate:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {name: 'out', text: '出局', title: '出局', icon: '', classname: 'btn btn-xs btn-danger btn-dialog', url: 'cartorderitem/edit',callback:function(data){}},
                                {name: 'detail', text: '', title: '', icon: 'fa fa-eye', classname: 'btn btn-xs btn-success btn-dialog', url: 'cartorderitem/detail',callback:function(data){}},
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status != 1){
                                    $(table).data("operate-out", null);
                                }
                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        out: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});